package com.atguigu.gmallpublisher.controller;

import com.atguigu.gmallpublisher.service.GmvService;
import com.atguigu.gmallpublisher.service.UvService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.logging.SimpleFormatter;

/**
 * @className: SugarController
 * @author: LinCong
 * @description:
 * @date: 2023/2/18 11:46
 * @version: 1.0
 */

@RestController
@RequestMapping("/api/suger")
public class SugarController {
    @Autowired
    private GmvService gmvService;
    @Autowired
    private UvService uvService;


    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            date = getToday();
        }
        Double gmv = gmvService.getGmv(date);

        return "{ " +
                "  \"status\": 0, " +
                "  \"msg\": \"\", " +
                "  \"data\": " + gmv +
                "}";
    }

    @RequestMapping("/ch")
    public String getUvByCh(@RequestParam(value = "date", defaultValue = "0") int date){
        if (date == 0) {
            date = getToday();
        }

        Map uvByCh = uvService.getUvByCh(date);
        Set chs = uvByCh.keySet();
        Collection uvs = uvByCh.values();

        return "{ " +
                "  \"status\": 0, " +
                "  \"msg\": \"\", " +
                "  \"data\": { " +
                "    \"categories\": [ \"" +
                StringUtils.join(chs,"\",\"")+
                "\"    ], " +
                "    \"series\": [ " +
                "      { " +
                "        \"name\": \"渠道\", " +
                "        \"data\": [ " +
                StringUtils.join(uvs,",")+
                "        ] " +
                "      } " +
                "    ] " +
                "  } " +
                "}";
    }

    private int getToday() {
        long ts = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(sdf.format(ts));
    }
}
